#include <iostream>
#include "operand.h"
#include "tree.h"
#include "line.h"
#include "engine.h"

using namespace std;

void Engine::Clock(int cycle)
{
	cout << "===================== ( Cycle " << cycle << " ) ======================" << endl;
	this->UpdateStates(cycle);
	this->Execution(cycle);
	this->PrintTrace(cycle);
}

void Engine::UpdateStates(int cycle)
{
	for ( vector<Operand*>::iterator it = operands->begin(); it != operands->end(); it++ )
	{
		(*it)->UpdateState();
	}
}

void Engine::PrintTrace(int cycle)
{
	cout << "Variables print:" << endl;
	for ( vector<Operand*>::iterator it = operands->begin(); it != operands->end(); it++ )
	{
		(*it)->PrintTrace();
	}
}

void Engine::Execution(int cycle)
{
	for ( vector<Line*>::iterator itLine = operations->begin(); itLine != operations->end(); itLine++ )
	{
		(*itLine)->Execute(cycle);
	}
}

void Engine::Simulate()
{
	for(int cycle = 0; cycle < this->max_clock; cycle++)
	{
		this->Clock(cycle);
	}
}
